package com.macken.wowadmin.dao;

import java.util.List;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.macken.wowadmin.entity.HostInfo;
import com.macken.wowadmin.util.DaoSupport;

public class HostDao {
	public static HostDao instance = new HostDao();
	private HostDao() {

	}

	public List<HostInfo> getHostList() {
		String sql = "select * from host_info order by host_id ";
		return DaoSupport.db.query(sql, HostInfo.rowMapper);
	}

	public HostInfo getHostById(int hostId) {
		String sql = "select * from host_info where host_id=?";
		List<HostInfo> list = DaoSupport.db.query(sql, HostInfo.rowMapper, hostId);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public boolean add(HostInfo host) {
		String sql = "insert into host_info(ip,port,note) value(:ip,:port,:note)";
		int rows = DaoSupport.db.getNamedParameterJdbcOperations()
				.update(sql, new BeanPropertySqlParameterSource(host));
		return rows > 0;
	}

	
	public boolean update(HostInfo host) {
		String sql = "update host_info set ip=?,port=?,note=? where host_id=?";
		int rows = DaoSupport.db.update(sql, host.getIp(), host.getPort(), host.getNote(), host.getHostId());
		return rows > 0;
	}
	
	public boolean del(int hostid){
		String sql="delete from host_info where host_id=?";
		int rows=DaoSupport.db.update(sql, hostid);
		return rows>0;
	}

	public static void main(String[] args) {
		HostDao dao = HostDao.instance;
		dao.getHostList();
	}

}
